<?php

namespace app\api\controller\stall;

use app\api\controller\Controller;
use app\api\model\order\Order as OrderModel;
use app\api\model\stall\Apply as ApplyModel;
use app\api\model\stall\Product as ProductModel;
use app\common\exception\BaseException;
use app\Request;

/**
 * 摊位申请
 */
class Apply extends Controller
{
    // 当前用户
    /**
     * @var
     */
    private $user;

    /**
     * 构造方法
     */
    public function initialize()
    {
        $this->user = $this->getUser();   // 用户信息
    }

    public function index(Request $request)
    {
        $data = $request->param();
        //获取店铺信息
        $detail = ApplyModel::getdetail($data['stall_id']);
        if (!$detail) {
            return $this->renderError('店铺不存在');
        }
        $monthNums = ProductModel::getStallSaleTime($detail['apply_id'],1);
        return $this->renderSuccess('', compact('detail','monthNums'));
    }

    /**
     * 提交申请
     */
    public function submit()
    {
        $data = $this->postData();
        if (empty($data['real_name']) || empty($data['mobile'])) {
            throw new BaseException(['msg' => '姓名或者手机号为空']);
        }
        if (!isset($data['store_name']) || $data['store_name'] == '') {
            throw new BaseException(['msg' => '店铺名称不能为空']);
        }
        $model = new ApplyModel;
        $havid = $model->where('user_id=' . $this->user['user_id'])->where('apply_status=10')->find();
        if ($havid) {
            return $this->renderError('请耐心等待，不要重复提交申请');
        }
        if ($model->submit($this->user, $data)) {
            return $this->renderSuccess('成功');
        }
        return $this->renderError($model->getError() ?: '提交失败');
    }

    /**
     * 申请状态
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Author joey
     * @date 2024/3/30 14:37
     */
    public function status()
    {
        $result = ApplyModel::status($this->user['user_id']);
        return $this->renderSuccess('', compact('result'));
    }

    public function address(Request $request)
    {
        $data = $request->param();
        //获取店铺信息
        $detail = ApplyModel::getdetail($data['stall_id']);
        if (!$detail) {
            return $this->renderError('店铺不存在');
        }
        $detail['detail'] = $detail['address'];
        return $this->renderSuccess('', compact('detail'));
    }

    public function myinfo()
    {
        $user = $this->getUser();
        //获取店铺信息
        $detail = ApplyModel::getdetail(['user_id'=>$user['user_id'],'apply_status'=>20]);
        if (!$detail) {
            return $this->renderError('店铺不存在');
        }
        $YNums = ProductModel::getStallSaleTime($detail['apply_id'],1);
        $YMoney = ProductModel::getStallSaleTime($detail['apply_id'],1,1);
        $AllMoney = ProductModel::getStallSaleTime($detail['apply_id'],0,1);
        return $this->renderSuccess('', compact('detail','YNums','YMoney','AllMoney'));
    }

    public function changemyinfo()
    {
        $data = $this->postData();
        $user = $this->getUser();
        $detail = ApplyModel::getdetail(['user_id'=>$user['user_id'],'apply_status'=>20]);
        if (!$detail) {
            return $this->renderError('店铺不存在');
        }
        $myshop_supplier_id = $detail['shop_supplier_id'];
        $address = ['longitude'=>$data['longitude'],'latitude'=>$data['latitude']];
        $checkPTS = OrderModel::checkAddrPointToSupplier($address,$myshop_supplier_id);
        if(!$checkPTS){
            return $this->renderError('地址不在店铺配送范围');
        }
        $data = [
            'address'      => isset($data['address']) ? trim($data['address']) : '',
            'house_number' => isset($data['house_number']) ? trim($data['house_number']) : '',
            'longitude' => $data['longitude']??'',
            'latitude' => $data['latitude']??'',
        ];
        if ($detail->save($data)) {
            return $this->renderSuccess('修改成功');
        }
        return $this->renderError('提交失败');
    }
}